package tomsung.cloud.agdc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tomsung.cloud.agdc.bean.OperateLog;
import tomsung.cloud.agdc.model.JsonObject;
import tomsung.cloud.agdc.model.JsonRequest;
import tomsung.cloud.agdc.model.JsonResponse;
import tomsung.cloud.agdc.service.OperateLogService;

@Controller
public class OperLogMgrController extends CommonController{
	
private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private OperateLogService logService;
	
	@ResponseBody
	@RequestMapping(value = "/log", method = RequestMethod.POST)
	protected JsonObject transer(final HttpServletRequest request, @RequestBody JsonRequest req) {
		return super.transer(request, req);
	}

	/**
	 * 获取所有作物信息
	 * @return
	 */
	public JsonResponse GET_OPERLOG(JsonRequest req) {
		JsonResponse res = req.getRep();
		try
		{
			String name = (String) req.V.get("USERNAME");
			int pageIndex = (Integer) req.V.get("INDEX");
			int pageStep = (Integer) req.V.get("STEP");
			List<OperateLog> logs = new ArrayList<OperateLog>();
			int count = 0;
			if (name != null && ""!=name)
			{
				logs = logService.selectOperateLog(name, pageIndex, pageStep);
				count = logService.selectOperateLogCount(name);
			}
			else
			{
				logs = logService.selectOperateLog(null, pageIndex, pageStep);
				count = logService.selectOperateLogCount(null);
			}
			res.V.put("LOGS", logs);
			res.V.put("COUNT", count);
		}
		catch(Exception e)
		{
			res.R = false;
			logger.error(e.getMessage(), e);
		}
		return res;
	}
}
